package com.longqin.wechat.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.longqin.wechat.entity.BaseResponse;
import com.longqin.wechat.entity.MyException;

/**主要用于捕捉jwt验证异常
 * @author longqin
 *
 */
@RestController
public class MyErrorController implements ErrorController{
	
	@Override
	public String getErrorPath() {
		return "/error";
	}
	
	@RequestMapping("/error")
	public BaseResponse handleError(HttpServletRequest request){
		Integer statusCode=(Integer) request.getAttribute("javax.servlet.error.status_code");
		Exception attribute=(Exception) request.getAttribute("javax.servlet.error.exception");
		if(null!= attribute && attribute.getCause() instanceof MyException){
			MyException we= (MyException) attribute.getCause();
			return we.getError();
		}else{
			return new BaseResponse(statusCode, "未知错误");
		}
		
	}
	
}

